package com.awspaas.user.apps.cpq.controller;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.commons.mvc.view.ResponseObject;
import com.actionsoft.bpms.server.UserContext;
import com.actionsoft.sdk.local.SDK;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

public class CPQ_GetLaborCostWeb {

	public String getLaborCost(UserContext userContext, String bindId, String datas, String dataId) {
		try {
			JSONArray list=JSONArray.parseArray(datas);
			double totals=0;
			String msg="";
			for (Object object : list) {
				String str=String.valueOf(object);
				JSONObject	json=JSONObject.parseObject(str);
		//{"NUMBER":"3","PRICE":"200","$data":{"NUMBER":"","PRICE":"","TOTAL_PRICE":"","NAME":""},"PRICE_DISPLAYVALUE":"","NAME":"打屡工","BINDID":"07fb8dae-4fab-4853-81fa-1448c995e8af","NAME_DISPLAYVALUE":"","$dataExtend":{"NUMBER":{"displayValue":"3","extends":{}},"PRICE":{"displayValue":"200","extends":{}},"TOTAL_PRICE":{"displayValue":"","extends":{}},"NAME":{"displayValue":"打屡工","extends":{}}},"TOTAL_PRICE":"","NUMBER_DISPLAYVALUE":"","ID":"ac8ef2db-6ef1-4d9f-b04b-dba296caa32e","TOTAL_PRICE_DISPLAYVALUE":"","FORM_EDITGRID_DATA_ATTR_COLUMN":{"isCreate":true},"PROCESSDEFID":"obj_8bbaa49336d54f218d8b60379dcb8b68"}
				String name= json.getString("NAME");
				if(!name.isEmpty()) {
					   String number= json.getString("NUMBER").isEmpty()?"0":json.getString("NUMBER");
		               String price= json.getString("PRICE").isEmpty()?"0":json.getString("PRICE");		               
		               double p=Double.parseDouble(price);
		               double m=Double.parseDouble(number);
		               //总价
		              double total= p*m;
		              totals+=total;
		              //工艺说明
		              msg+="工艺："+name+"，单价:"+price+"，数量:"+number+";";					
				}
				
			}
			BO bo=SDK.getBOAPI().query("BO_EU_CPQ_MODULE_MSG_GRID").addQuery("ID=", dataId).detail();
			bo.set("RGF",totals );
			bo.set("GYSM",msg );
			SDK.getBOAPI().update("BO_EU_CPQ_MODULE_MSG_GRID", bo);
			ResponseObject ro = ResponseObject.newOkResponse();
			return ro.toString();
		} catch (Exception e) {
			throw e;
		}
		
	}

}
